<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="通用能力名称" prop="generalName">
        <el-input v-model="form.generalName" placeholder="请输入通用能力名称" clearable/>
      </el-form-item>
      <el-form-item label="能力描述" prop="generalDescribe">
        <el-input v-model="form.generalDescribe" placeholder="请输入能力描述" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="loading" type="primary" size="mini" @click="save">保 存</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {insertGeneralAbility, getGeneralAbilityById, updateGeneralAbility} from "@/api/assess/examGeneralAbility"
  export default {
    data() {
      return {
        loading: false,
        form: {},
        rules: {
          generalName: [
            {required: true, message: "通用能力不能为空", trigger: "blur"}
          ],
          generalDescribe: [
            {required: true, message: "能力描述不能为空", trigger: "blur"}
          ],
        }
      }
    },
    methods: {

      // 初始化
      init(id) {
        getGeneralAbilityById(id).then(response => {
          this.form = response.responseBody
        })
      },

      // 保存
      save() {
        this.$refs['form'].validate(valid => {
          if(valid) {
            // 判断是新增还是修改
            if (this.form.id) {
              updateGeneralAbility(this.form).then(response => {
                if (response.retCode === '200'){
                  this.$message.success(response.message)
                  this.$emit('cancel')
                }
              })
            } else {
              insertGeneralAbility(this.form).then(response => {
                if (response.retCode === '200') {
                  this.$message.success(response.message)
                  this.$emit('cancel')
                }
              })
            }
          }
        })
      },
      cancel() {
        this.$emit('cancel')
      }
    }
  }
</script>